package com.example.administrator.base.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.dalimao.corelibrary.VerificationCodeInput;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Ring extends BaseActivity {

    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    String carnumber = "";
    SweetAlertDialog sweetAlertDialog;
    @InjectView(R.id.number)
    VerificationCodeInput number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ring);
        ButterKnife.inject(this);
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("车锁响铃");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        number.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
            public void onComplete(String s) {
                carnumber = s;
            }
        });
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {

    }

    @OnClick(R.id.ib_ring)
    public void onViewClicked() {
        if (carnumber.equals("")) {
            showToast("请输入车号");
            return;
        }
        if (carnumber.length() != 9) {
            showToast("请输入完整车号");
            return;
        }
        sweetAlertDialog.show();
        ring(carnumber);
    }


    /*响铃*/
    public void ring(String number) {
        String id = String.valueOf(SharedPreferencesUtils.getParam(Ring.this, "adminId", ""));
        Log.i("454151", "id:" + id);
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("action", "BELLLOCK")
                .add("adminId", id)
                .add("bicycleNo", number)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/operationperson?")
                .post(formBody)
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Log.i("789215", string);
                        final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                        if (mse != null) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(Ring.this, "" + mse.getMessage(), Toast.LENGTH_LONG).show();
                                    sweetAlertDialog.dismiss();
                                    /*finish();*/
                                }
                            });

                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(Ring.this, "数据异常请重试", Toast.LENGTH_LONG).show();
                                    sweetAlertDialog.dismiss();

                                }
                            });
                        }
                    } else {
                        throw new Exception("Unexpected code " + response);
                    }
                } catch (Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sweetAlertDialog.dismiss();
                        }
                    });
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
